<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

require_once(dirname(__FILE__) . "/../../dbcom/UserCom.php");
require_once(dirname(__FILE__) . "/../../dbcom/CustomerCom.php");
require_once(dirname(__FILE__) . "/../../common/Common.php");
require_once(dirname(__FILE__) . "/../../dbcom/OrderCom.php");
require_once(dirname(__FILE__) . "/../../dbcom/AppointCom.php");
require_once(dirname(__FILE__) . "/../../dbcom/ReservetimeCom.php");
require_once(dirname(__FILE__) . "/../../dbcom/RagionCom.php");
require_once(dirname(__FILE__) . "/../../dbcom/UsertypeCom.php");

$usercom = new UserCom();
$customercom = new CustomerCom();
$common = new Common();
$ordercom = new OrderCom();
$appointcom = new AppointCom();
$reservetimecom = new ReservetimeCom();
$ragioncom = new RagionCom();
$usertypecom = new UsertypeCom();

$userid = $common->getLogin();
if (!isset($userid) || empty($userid)) {
    header('Location:./../login.php');
}
//获取分页
if (empty($_REQUEST['p']) || !isset($_REQUEST['p'])) {
    $page = 1;
    $minlimit = 0;
} else {
    $page = $_REQUEST['p'];
    if ($page > 1) {
        $minlimit = 50 * $page;
    } else {
        $minlimit = 0;
    }
}
$limitnum = 50;

$user = $usercom->getUserById($userid);
if ($user['role'] != 0) {
    $appontarr = $appointcom->getAllwAppoint($user['community_id'], $minlimit, $limitnum);
    $number = $appointcom->getAllwNum($user['community_id']);
} else {
    $appontarr = $appointcom->getAllAppoint($minlimit, $limitnum);
    $number = $appointcom->getAllNum();
}

$p = (int) ($number / 50 + 1);

$res = array();
if (!empty($appontarr)) {
    foreach ($appontarr as $k => $v) {
        $res[$k]['id'] = $v['id'];
        $res[$k]['username'] = $v['username'];
        $res[$k]['mobile'] = $v['mobile'];
        $res[$k]['address'] = $v['address'];
        switch ($v['status']) {
            case 0:
                $res[$k]["status"] = "等待工作人员确认";
                break;
            case 1:
                $res[$k]["status"] = "预约成功";
                break;
            case 2:
                $res[$k]["status"] = "预约取消";
                break;
            default :
                $res[$k]["status"] = "等待工作人员确认";
                break;
        }
        $aa = $reservetimecom->getRtimeById($v['reservetime_id']);
        $time = date("m-d", $v['time']);
        $res[$k]["reservetime"] = $time . " " . $aa['info'];
    }
}
?>
<!DOCTYPE html>
<html>

    <head>

        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="keywords" content="">
        <meta name="description" content="">
        <link href="../public/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
        <link href="../public/css/font-awesome.css?v=4.4.0" rel="stylesheet">

        <link href="../public/css/animate.css" rel="stylesheet">
        <link href="../public/css/style.css?v=4.1.0" rel="stylesheet">

        <link href="../public/css/paging.css" rel="stylesheet">
        <link href="../public/css/showproject.css" rel="stylesheet">
    </head>

    <body class="gray-bg">

        <div class="wrapper wrapper-content animated fadeInUp">
            <div class="row">
                <div class="col-sm-12">

                    <div class="ibox">
                        <div class="ibox-title">
                            <h5>订单管理</h5>
                            <div class="ibox-tools">

                            </div>
                        </div>
                        <div class="ibox-content">
                            <div class="row m-b-sm m-t-sm">
                                <div class="col-md-1">

                                </div>
                                <div class="col-md-11">
                                    <div class="input-group">

                                    </div>
                                </div>
                            </div>

                            <div class="project-list">

                                <div class="project-list">
                                    <div class="table-responsive">
                                        <table class="table table-striped">
                                            <thead>
                                                <tr>
                                                    <td class="project-people">
                                                        <span>地址</span>
                                                    </td>
                                                    <td class="project-people">
                                                        <span>使用者姓名</span>
                                                    </td>
                                                    <td class="project-people">
                                                        <span>联系手机</span>
                                                    </td>
                                                    <td class="project-people">
                                                        <span>订单状态</span>
                                                    </td>
                                                    <td class="project-people">
                                                        <span>预约时间</span>
                                                    </td>
                                                    <td class="project-actions">
                                                        <span>操作</span>
                                                    </td>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php
                                                foreach ($res as $v) {
                                                    ?>
                                                    <tr id="list<?= $v['id'] ?>" data-id="<?= $v['id'] ?>">
                                                        <td class="project-people">
                                                            <span><?= $v['address'] ?></span>
                                                        </td>
                                                        <td class="project-people">
                                                            <span><?= $v['username'] ?></span>
                                                        </td>
                                                        <td class="project-people">
                                                            <span><?= $v['mobile'] ?></span>
                                                        </td>
                                                        <td class="project-people">
                                                            <span><?= $v['status'] ?></span>
                                                        </td>
                                                        <td class="project-people">
                                                            <span><?= $v['reservetime'] ?></span>
                                                        </td>
                                                        <td class="project-actions">
                                                            <a data-id="<?= $v['id'] ?>" data-toggle="modal" data-target="#myModal1" class="btn btn-white btn-sm btn-chk"><i class="fa fa-folder"></i> 查看 </a>
                                                            <a data-id="<?= $v['id'] ?>" class="btn btn-white btn-sm btn-det"><i class="fa fa-folder"></i> 删除 </a>
                                                        </td>
                                                    </tr>
                                                <?php } ?>
                                            </tbody>
                                        </table>
                                        <div class="box" id="box"></div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="modal fade" id="myModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" id="close1" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title" id="myModalLabel">查看预约详情</h4>
                    </div>
                    <div class="form-horizontal">
                        <div class="ibox-content">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">预约用户</label>
                                <div class="col-sm-10">
                                    <input type="text" id="customer" disabled="" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">地区</label>
                                <div class="col-sm-10">
                                    <input type="text" id="area" disabled="" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">街道</label>
                                <div class="col-sm-10">
                                    <input type="text" id="street" disabled="" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">社区</label>
                                <div class="col-sm-10">
                                    <input type="text" id="community" disabled="" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">详细地址</label>
                                <div class="col-sm-10">
                                    <input type="text" id="address" disabled="" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">使用者姓名</label>
                                <div class="col-sm-10">
                                    <input type="text" id="username" disabled="" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">身份证号</label>
                                <div class="col-sm-10">
                                    <input type="text" id="codeid" disabled="" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">性别</label>
                                <div class="col-sm-10">
                                    <input type="text" id="sex" disabled="" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">联系电话</label>
                                <div class="col-sm-10">
                                    <input type="text" id="mobile" disabled="" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">用户类型</label>
                                <div class="col-sm-10">
                                    <input type="text" id="usertype" disabled="" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">预约时间</label>
                                <div class="col-sm-10">
                                    <input type="text" id="reservetime" disabled="" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">满意度</label>
                                <div class="col-sm-10">
                                    <input type="text" id="stais" disabled="" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">意见回馈</label>
                                <div class="col-sm-10">
                                    <input type="text" id="staistext" disabled="" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">预约服务</label>
                                <div class="col-sm-10">
                                    <input type="text" id="service" disabled="" class="form-control">
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal -->
        </div>

        <!-- 全局js -->
        <script src="../public/js/jquery.min.js?v=2.1.4"></script>
        <script src="../public/js/bootstrap.min.js?v=3.3.6"></script>
        <script src="../public/js/plugins/layer/layer.min.js"></script>

        <!-- 自定义js -->
        <script src="../public/js/paging.js?v=1.0.0"></script>
        <script src="../public/js/upload.js?v=1.0.0"></script>
        <script src="../public/js/common.js?v=1.0.4"></script>

        <script>
            $(document).ready(function () {

                $('#loading-example-btn').click(function () {
                    btn = $(this);
                    simpleLoad(btn, true)

                    // Ajax example
                    //                $.ajax().always(function () {
                    //                    simpleLoad($(this), false)
                    //                });

                    simpleLoad(btn, false)
                });
            });

            function simpleLoad(btn, state) {
                if (state) {
                    btn.children().addClass('fa-spin');
                    btn.contents().last().replaceWith(" Loading");
                } else {
                    setTimeout(function () {
                        btn.children().removeClass('fa-spin');
                        btn.contents().last().replaceWith(" Refresh");
                    }, 2000);
                }
            }
            var setTotalCount = <?= $number ?>;
            $('#box').paging({
                initPageNo: <?= $page ?>, // 初始页码
                totalPages: <?= $p ?>, //总页数
                totalCount: '合计' + setTotalCount + '条数据', // 条目总数
                slideSpeed: 600, // 缓动速度。单位毫秒
                jump: true, //是否支持跳转
                callback: function (page) { // 回调函数
                    console.log(page);
                }
            });

            $(".btn-det").click(function () {
                var appointid = $(this).data("id");
                var datastr = "cont=1&appointid=" + appointid;
                $.ajax({
                    url: "/../../controller/order.php",
                    type: "POST",
                    data: datastr,
                    success: function (e) {
                        console.log(e);
                        var result = eval("(" + e + ")");
                        if (result.code == 0) {
                            layer.msg('删除成功', {
                                time: 2000, //20s后自动关闭
                                btn: ['OK']
                            });
                            $("#list" + result.data).css("display", "none");
                        } else {
                            layer.msg('删除失败请刷新重试', {
                                time: 2000, //20s后自动关闭
                                btn: ['OK']
                            });
                            return;
                        }
                        return;
                    }
                });
            });
            //初始化订单详情弹窗
            function cleanOrderdetail() {
                $("#customer").val("");
                $("#area").val("");
                $("#street").val("");
                $("#community").val("");
                $("#address").val("");
                $("#username").val("");
                $("#codeid").val("");
                $("#sex").val("");
                $("#mobile").val("");
                $("#usertype").val("");
                $("#stais").val("");
                $("#staistext").val("");
                $("#reservetime").val("");
                $("#service").val("");
                return;
            }
            $(".btn-chk").click(function () {
                cleanOrderdetail();
                var appointid = $(this).data("id");
                var datastr = "cont=2&appointid=" + appointid;
                $.ajax({
                    url: "/../../controller/order.php",
                    type: "POST",
                    data: datastr,
                    success: function (e) {
                        var result = eval("(" + e + ")");
                        if (result.code != 0) {
                            layer.msg('查找数据失败请刷新重试', {
                                time: 2000, //20s后自动关闭
                                btn: ['OK']
                            });
                            return;
                        }
                        $("#customer").val(result.data.customer);
                        $("#area").val(result.data.area);
                        $("#street").val(result.data.street);
                        $("#community").val(result.data.community);
                        $("#address").val(result.data.address);
                        $("#username").val(result.data.username);
                        $("#codeid").val(result.data.codeid);
                        $("#sex").val(result.data.sex);
                        $("#mobile").val(result.data.mobile);
                        $("#usertype").val(result.data.usertype);
                        $("#reservetime").val(result.data.reservetime);
                        $("#stais").val(result.data.stais);
                        $("#staistext").val(result.data.staistext);
                        $("#service").val(result.data.service);
                    }
                });
            });
        </script>

    </body>
</html>
